from flask_sqlalchemy import SQLAlchemy
from flask import Flask
import datetime
from flask_login import UserMixin
app = Flask(__name__)  # 实例化
app.config["SQLALCHEMY_DATABASE_URI"] = 'sqlite:///'+'DataBase.db'
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["SECRET_KEY"] = "2022"

db = SQLAlchemy(app)


# 用户信息表
class UserInformation(db.Model, UserMixin):
    __tablename__ = "user_information"
    user_id = db.Column(db.Integer, primary_key=True)
    user_name = db.Column(db.String(64), nullable=False, unique=True)
    user_password = db.Column(db.String(64), nullable=False)
    user_permission = db.Column(db.Enum("user", "admin"), nullable=False)

    charging_details = db.relationship(
        "ChargingDetail", backref="user_information")


# 充电详单信息
class ChargingDetail(db.Model):
    __tablename__ = "charging_detail"
    bill_id = db.Column(db.Integer, primary_key=True)
    bill_time = db.Column(
        db.DateTime, default=datetime.datetime.now())
    charging_pile_id = db.Column(db.String(64), db.ForeignKey("charging_pile_report.charging_pile_id"))
    charging_quantity = db.Column(db.Float)
    charging_duration = db.Column(db.BigInteger)
    starting_time = db.Column(db.DateTime)
    stopping_time = db.Column(db.DateTime)
    charging_cost = db.Column(db.Float)
    service_cost = db.Column(db.Float)
    total_cost = db.Column(db.Float)
    user_id = db.Column(db.Integer, db.ForeignKey("user_information.user_id"))
    complete = db.Column(db.Enum("no", "yes"), nullable=False)


class ChargingPileReport(db.Model):
    __tablename__ = "charging_pile_report"
    charging_pile_id = db.Column(db.Enum("1", "2", "3", "4", "5"), primary_key=True)
    cumulative_charging_times = db.Column(db.Integer, nullable=False)
    cumulative_charging_duration = db.Column(db.BigInteger)
    accumulative_charging_quantity = db.Column(db.Float)
    accumulative_charging_cost = db.Column(db.Float)
    accumulative_service_cost = db.Column(db.Float)
    accumulative_total_cost = db.Column(db.Float)
    showing_time = db.Column(
        db.DateTime, default=datetime.datetime.now())

    charging_details = db.relationship(
        "ChargingDetail", backref="charging_pile_report")


if __name__ == "__main__":
    db.create_all()
    # db.drop_all()
    for i in range(5):
        temp = ChargingPileReport(charging_pile_id=str(i+1), cumulative_charging_times=0, cumulative_charging_duration=0,
                                accumulative_charging_quantity=0, accumulative_charging_cost=0, accumulative_service_cost=0, accumulative_total_cost=0)
        db.session.add(temp)
        db.session.commit()
